package com.itany.book.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.exception.RequestParamsException;
import com.itany.exception.SupplierExistsException;
import com.itany.mapper.LevelMapper;
import com.itany.mapper.SupplierMapper;
import com.itany.pojo.Level;
import com.itany.pojo.Supplier;
import com.itany.service.SupplierService;

import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    LevelMapper levelMapper;
    /**
     * 不分页查询所有的供应商信息
     *
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Supplier> findAllNoPages() {
        List<Supplier> suppliers = supplierMapper.selectAll(null);
        return suppliers.isEmpty()?null:suppliers;
    }


    /**
     * 分页查找所有供应商
     * @param page 页数
     * @param rows 数据
     * @return
     * @throws RequestParamsException
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public PageInfo<Supplier> findAll(Integer page, Integer rows,String suppliernameKey) throws RequestParamsException {
        PageHelper.startPage(page, rows);
        List<Supplier> list;
        List<Level> levels = levelMapper.selectAll();
        list = supplierMapper.selectAll(StringUtils.escapeStr(suppliernameKey));
        if(!levels.isEmpty()&&!list.isEmpty()){
            String curLevel = levels.get(0).getName();
            for (Supplier supplier :list) {
                if(supplier.getTotalSellnumber() == null){
                    continue;
                }
                for (Level level: levels) {
                    if(level.getSection().equals(supplier.getTotalSellnumber())){
                        supplier.setLevel(level.getName());
                        break;
                    }else if(level.getSection() < supplier.getTotalSellnumber()){
                        curLevel = level.getName();
                    }else {
                        supplier.setLevel(curLevel);
                    }

                }
            }
        }
        return new PageInfo<>(list);
    }

    /**
     * 添加供应商
     *
     * @param suppliername 名称
     * @param linkman    联系人
     * @param phone        电话
     * @param info         公司信息
     * @return
     */
    @Override
    public void addSupplier(String suppliername, String linkman, String phone, String info) throws RequestParamsException {
        if(StrUtil.isEmptyIfStr(suppliername)||StrUtil.isEmptyIfStr(linkman)||StrUtil.isEmptyIfStr(phone)||StrUtil.isEmptyIfStr(info)){
            throw new RequestParamsException("请求参数缺失");
        }

        Supplier supplier = new Supplier();
        supplier.setName(suppliername);
        supplier.setLinkman(linkman);
        supplier.setPhone(phone);
        supplier.setInfo(info);
        supplier.setCreatetime(new Date());
        supplier.setFlag(Constant.DISABLE_STATUS);
        supplierMapper.insertSupplier(supplier);
    }

    /**
     * 添加供应商
     *
     * @param id           供应商id
     * @param suppliername 名称
     * @param linkman      联系人
     * @param phone        电话
     * @param info         公司信息
     * @return
     */
    @Override
    public void modifySupplier(String id, String suppliername, String linkman, String phone, String info) throws RequestParamsException, SupplierExistsException {
        if(StrUtil.isEmptyIfStr(id)){
            throw new RequestParamsException("请求参数缺失");
        }
        Integer idInt;
        try {
            idInt = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
        Supplier supplier = supplierMapper.selectByName(suppliername);
        if(supplier != null && !supplier.getId().equals(idInt)){
            throw new SupplierExistsException("该经销商已经存在");
        }
        supplier = new Supplier();
        supplier.setName(suppliername);
        supplier.setLinkman(linkman);
        supplier.setPhone(phone);
        supplier.setInfo(info);
        supplier.setId(idInt);
        supplierMapper.updateSupplier(supplier);
    }


    /**
     * 修改指定供应商状态
     *
     * @param id     供应商id
     * @param status 更新后的状态
     */
    @Override
    public void modifyStatus(String id, Integer status) throws RequestParamsException {
        if (StrUtil.isEmpty(id) || status == null) {
            throw new RequestParamsException("请求参数缺失");
        }
        if (!Constant.ENABLE_STATUS.equals(status) && !Constant.DISABLE_STATUS.equals(status)) {
            throw new RequestParamsException("请求参数格式有误");
        }
        try {
            supplierMapper.updateStatus(Integer.parseInt(id), status);
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
    }
}
